package com.eshop.handler;

import org.slf4j.LoggerFactory;
import java.util.Objects;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.http.HttpStatus;
import com.eshop.exception.EntityNotFoundException;
import com.eshop.exception.EntityExistException;
import com.eshop.exception.ErrorRequestException;
import com.eshop.exception.BadRequestException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import com.eshop.utils.ThrowableUtil;
import org.springframework.http.ResponseEntity;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler
{
    private static final Logger log;
    
    @ExceptionHandler({ Throwable.class })
    public ResponseEntity<ApiError> handleException(final Throwable e) {
        GlobalExceptionHandler.log.error(ThrowableUtil.getStackTrace(e));
        return this.buildResponseEntity(ApiError.error(e.getMessage()));
    }
    
    @ExceptionHandler({ BadCredentialsException.class })
    public ResponseEntity<ApiError> badCredentialsException(final BadCredentialsException e) {
        final String message = "坏的凭证".equals(e.getMessage()) ? "用户名或密码不正确" : e.getMessage();
        GlobalExceptionHandler.log.error(message);
        return this.buildResponseEntity(ApiError.error(message));
    }
    
    @ExceptionHandler({ BadRequestException.class })
    public ResponseEntity<ApiError> badRequestException(final BadRequestException e) {
        GlobalExceptionHandler.log.error(ThrowableUtil.getStackTrace((Throwable)e));
        return this.buildResponseEntity(ApiError.error(e.getStatus(), e.getMessage()));
    }
    
    @ExceptionHandler({ ErrorRequestException.class })
    public ResponseEntity<ApiErr> errorRequestException(final ErrorRequestException e) {
        GlobalExceptionHandler.log.error(ThrowableUtil.getStackTrace((Throwable)e));
        final ApiErr apiError = new ApiErr(e.getStatus(), e.getMessage());
        return this.buildResponseEntity2(apiError);
    }
    
    @ExceptionHandler({ EntityExistException.class })
    public ResponseEntity<ApiError> entityExistException(final EntityExistException e) {
        GlobalExceptionHandler.log.error(ThrowableUtil.getStackTrace((Throwable)e));
        return this.buildResponseEntity(ApiError.error(e.getMessage()));
    }
    
    @ExceptionHandler({ EntityNotFoundException.class })
    public ResponseEntity<ApiError> entityNotFoundException(final EntityNotFoundException e) {
        GlobalExceptionHandler.log.error(ThrowableUtil.getStackTrace((Throwable)e));
        return this.buildResponseEntity(ApiError.error(HttpStatus.NOT_FOUND.value(), e.getMessage()));
    }
    
    @ExceptionHandler({ MethodArgumentNotValidException.class })
    public ResponseEntity<ApiError> handleMethodArgumentNotValidException(final MethodArgumentNotValidException e) {
        GlobalExceptionHandler.log.error(ThrowableUtil.getStackTrace((Throwable)e));
        final String[] str = Objects.requireNonNull(e.getBindingResult().getAllErrors().get(0).getCodes())[1].split("\\.");
        String message = e.getBindingResult().getAllErrors().get(0).getDefaultMessage();
        final String msg = "不能为空";
        if (msg.equals(message)) {
            message = str[1] + ":" + message;
        }
        return this.buildResponseEntity(ApiError.error(message));
    }
    
    private ResponseEntity<ApiError> buildResponseEntity(final ApiError apiError) {
        return (ResponseEntity<ApiError>)new ResponseEntity((Object)apiError, HttpStatus.valueOf((int)apiError.getStatus()));
    }
    
    private ResponseEntity<ApiErr> buildResponseEntity2(final ApiErr apiError) {
        return (ResponseEntity<ApiErr>)new ResponseEntity((Object)apiError, HttpStatus.valueOf((int)apiError.getStatus()));
    }
    
    static {
        log = LoggerFactory.getLogger((Class)GlobalExceptionHandler.class);
    }
}
